package com.upcode.weighing.detect.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.detect.service.OverBlacklistService;
import com.upcode.weighing.domain.OverBlacklist;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.vo.req.OverBlacklistAddVo;
import com.upcode.weighing.vo.req.OverBlacklistEditVo;
import com.upcode.weighing.vo.req.OverBlacklistQueryParam;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.resp.BaseResponseVo;
import com.upcode.weighing.vo.resp.OverBlacklistRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author alex
 * @since 2024-07-21
 */
@Controller
@RequestMapping("/overblacklist")
@Api(value = "/overblacklist",description = "治超名单接口",produces = MediaType.ALL_VALUE,tags = {"治超名单接口"})
public class OverBlacklistController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(OverBlacklistController.class);
    @Autowired
    private OverBlacklistService overBlacklistService;
    /**
     * 新增系统账号
     */
    @OpsLog(operation = "治超名单接口/添加名单" ,type = 2)
    @PostMapping("/add")
    @ApiOperation(value = "添加名单", notes = "添加", httpMethod = "POST")
    public Object add(@Validated @RequestBody OverBlacklistAddVo overBlacklistAddVo) {
        OverBlacklist overBlacklist=new OverBlacklist();
        overBlacklist.setStatus(1);
        BeanUtils.copyProperties(overBlacklistAddVo,overBlacklist);
        overBlacklist.setId(IdWorker.getId());
        try{
            overBlacklistService.saveOrUpdate(overBlacklist);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return error(e.getMessage());
        }

        return success("添加治超名单成功");
    }
    /**
     * 获取检测记录分页列表
     */
    @OpsLog(operation = "治超名单接口/分页查询" ,type = 1)
    @PostMapping("/queryPage")
    @ApiOperation(value = "名单分页查询",notes = "")
    public ResponseEntity<BaseResponseVo<IPage<OverBlacklistRespVo>>> queryPage(@RequestBody PageReqVo<OverBlacklistQueryParam> queryParamPageReqVo){
        IPage<OverBlacklistRespVo> page = overBlacklistService.queryPageList(queryParamPageReqVo);
        return HttpResponse.success(page);
    }

    /**
     *查询账号详情
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "治超名单接口/查看详情" , notes = "")
    public  ResponseEntity<BaseResponseVo<OverBlacklist>> details(@PathVariable("id")@NotNull(message = "id 不能为空") Long userId){
        OverBlacklist overBlacklist = overBlacklistService.getById(userId);
        return HttpResponse.success(overBlacklist);
    }

    /**
     * 根据id修改信息
     */
    @OpsLog(operation = "治超名单接口/撤销治超名单" ,type = 4)
    @PostMapping("/cancel")
    @ApiOperation(value = "撤销治超名单" ,notes = "")
    public Object cancel(@Validated @RequestBody OverBlacklistEditVo overBlacklistEditVo){
        OverBlacklist overBlacklist = overBlacklistService.getById(overBlacklistEditVo.getId());
        overBlacklist.setStatus(0);
        overBlacklist.setCancelReason(overBlacklistEditVo.getCancelReason());
        try {
            if(!overBlacklistService.updateById(overBlacklist)){
                return error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return success("修改成功");
    }

    /**
     * 根据id修改信息
     */
    @OpsLog(operation = "治超名单接口/更新治超名单" ,type = 4)
    @PostMapping("/update")
    @ApiOperation(value = "更新治超名单" ,notes = "")
    public Object updateById(@Validated @RequestBody OverBlacklistEditVo overBlacklistEditVo){
        OverBlacklist overBlacklist=new OverBlacklist();
        BeanUtils.copyProperties(overBlacklistEditVo,overBlacklist);
        try {
            if(!overBlacklistService.updateById(overBlacklist)){
                return error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        return success("修改成功");
    }

    /**
     * 根据id修改删除
     */
    @OpsLog(operation = "治超名单接口/删除" ,type = 4)
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "检测记录删除" ,notes = "")
    public Object delete(@PathVariable("id")@NotNull(message = "id 不能为空") Long id){
        try {
            if(!overBlacklistService.removeById(id)){
                return error("删除失败");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return error(e.getMessage());
        }
        return success("删除成功");
    }

}
